package com.company.solohibernate.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.company.modelo.Student;
import com.company.solohibernate.util.HibernateUtil;

public class HibernateStudentDao implements StudentDao {

	private Session session;
	
	public Student getStudent(Integer id) {
		Student student = null;
		 Session session = HibernateUtil.getSessionFactory().openSession();
		 Transaction tx = null;
		 try {
			  tx = session.beginTransaction();
			  student = (Student) session.get(Student.class, new Integer(id));
			  tx.commit();
			  tx = null;
		 } catch (HibernateException e) {
		   if (tx != null)
			   tx.rollback();
		   throw e;
		 } finally {
		  session.close();
		 }
	  return student;
	}

	public List<Student> getAllStudents() {		
		 List<Student> allStudents = new ArrayList<Student>();		 
		 Session session = HibernateUtil.getSessionFactory().openSession();
		 Transaction tx = null;
		 try {
			  tx = session.beginTransaction();
			  Query query = session.createQuery("from Student std order by std.lastName, std.firstName");
			  allStudents = query.list();
			  tx.commit();
			  tx = null;
		 } catch (HibernateException e) {
		   if (tx != null)
			   tx.rollback();
		   throw e; 
		 } finally {
		  session.close();
		 }
	  return allStudents;
	}

	public List<Student> getGraduatedStudents() {
		 List<Student> graduatedStudents = new ArrayList<Student>();	
		 Session session = HibernateUtil.getSessionFactory().openSession();
		 Transaction tx = null;
		 try {
			  tx = session.beginTransaction();
			  Query query = session.createQuery("from Student std where std.status=1");
			  graduatedStudents = query.list();
			  tx.commit();
			  tx = null;
		 } catch (HibernateException e) {
		  if (tx != null)
			  tx.rollback();
		  throw e;
		 } finally {
		  session.close();
		 }
	 return graduatedStudents;
	}

	public List<Student> findStudents(String lastName) {
		 //Collection students = null;
		 List<Student> students = new ArrayList<Student>();	
		 Session session = HibernateUtil.getSessionFactory().openSession();
		 Transaction tx = null;
		 try {
			  tx = session.beginTransaction();
			  Query query = session.createQuery("from Student std where std.lastName like ?");
			  query.setString(1, lastName + "%");
			  students = query.list();
			  tx.commit();
			  tx = null;
		  } catch (HibernateException e) {
			   if (tx != null)
				   tx.rollback();
			   throw e;
		  } finally {
		   session.close();
		  }
	 return students;
	}

	public void saveStudent(Student std) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		 Transaction tx = null;
		 try {
			  tx = session.beginTransaction();
			  session.saveOrUpdate(std);
			  tx.commit();
			  tx = null;
		  } catch (HibernateException e) {
			   if (tx != null)
				   tx.rollback();
			   throw e;
		  } finally {
		   session.close();
		  }
	}

	public void removeStudent(Student std) {
		 Session session = HibernateUtil.getSessionFactory().openSession();
		 Transaction tx = null;
		 try {
			  tx = session.beginTransaction();
			  session.delete(std);
			  tx.commit();
			  tx = null;
		  } catch (HibernateException e) {
			   if (tx != null)
				   tx.rollback();
			   throw e;
		  } finally {
			  session.close();
		  }
		
	}

}
